<?php

    /**
    * http://13styles.com/category/css-menus/ — Time 4 Bed CSS Menu
    * http://www.izzymenu.com/ — Create professional looking CSS menus for your Website as easy as never before!
    * http://dc2009.drupalcon.org/ — nice site
    * http://www.webdesignerwall.com/trends/best-of-css-design-2008/ — Best of CSS Design 2008
    * http://creativecommons.org/ — Creative Commons is a nonprofit organization that increases sharing and improves collaboration.
    * http://blogger.com/
    * http://service.futuremark.com/peacekeeper/results.action?key=25SJ
    */

    // добавление обработчика управляющей инструкции
    $mikron->OperatingInstructions->AddFunction('tablist', 'ShowTabList');
    // $mikron->JS->AddFile(dirname(__FILE__)."/script.js", "Скрипты модуля TabControl");
    // добавление необходимых сущностей
    $mikron->Schema->Add(dirname(__FILE__)."/schema.xml", "Схема модуля TabControl");

    // $mikron->CSS->AddFile(dirname(__FILE__)."/style.css", "Стиль модуля TabControl");
    $mikron->CSS->AddFile(dirname(__FILE__).'/templates/tabs0/style.css', "Стиль модуля TabControl 0");
    $mikron->CSS->AddFile(dirname(__FILE__).'/templates/tabs5/style.css', "Стиль модуля TabControl 5");
    $mikron->CSS->AddFile(dirname(__FILE__).'/templates/tabsB/style.css', "Стиль модуля TabControl B");
    $mikron->CSS->AddFile(dirname(__FILE__).'/templates/tabsD/style.css', "Стиль модуля TabControl D");
    $mikron->CSS->AddFile(dirname(__FILE__).'/templates/tabs2/style.css', "Стиль модуля TabControl 2");
    $mikron->CSS->AddFile(dirname(__FILE__).'/templates/menu8/style.css', "Стиль модуля TabControlMenu 8");
    $mikron->CSS->AddFile(dirname(__FILE__).'/templates/menu10/style.css', "Стиль модуля TabControlMenu 10");
    $mikron->CSS->AddFile(dirname(__FILE__).'/style.css', "Главный стиль модуля TabControlMenu");

    $is_ajax = isset($_GET['ajaxgettablist']) && isset($_GET['ajaxtabid']);

    if($is_ajax)
    {
        $out = ob_get_clean();
        $tabname = $_GET['ajaxgettablist'];
        $tabitemid = $_GET['ajaxtabid'];
        $_SESSION[$tabname.'_page'] = $tabitemid;
        $php_code_for_eval = "ShowTabList(Array('name'=>'$tabname'));";
        echo translateOpIns('{{$BODY}}', CatchFunction('eval2'));
        exit;
    }

    class tab_list
    {
        private static $styleisset = false;
        private $ttabs = Array();
        private $id = null;
        private $curpage = null;
        private $tablisttbl = null;
        private static $tabcount = 0;
        function __construct($tabid, $tabs, $with_ajax, $width = null, $style = null)
        {
            global $is_ajax;
            if(!self::$styleisset && !$is_ajax)
            {
                self::$styleisset = true;
                if($with_ajax)
                {
                    ?>
                    <script type="text/javascript">
                        /* AJAX */
                        function LoadAjaxTabItem(container, tabid, tabitemid)
                        {
                            x_container = container;
                            x_tabid=tabid;
                            x_tabitemid=tabitemid;
                            <?php
                                $a = $_SERVER['REQUEST_URI'];
                                $a = str_replace('?'.$_SERVER['QUERY_STRING'], null, $a);
                            ?>
                            getDataAjax("<?php echo $a; ?>?ajax=true", container, "ajaxgettablist="+tabid+"&ajaxtabid="+tabitemid, alertContents);
                            return false;
                        }
                        // получение данных, используя AJAX
                        // здесь реализовано на jquery, но можно и  по другому
                        function getDataAjax(url, container, data, fnctn)
                        {
                            $.ajax({type: "GET", url: url, data: data, success: function(data) {fnctn(data, container);}});
                        }
                        function alertContents(data, container)
                        {
                            var ocontainer = document.getElementById(container);
                            ocontainer.innerHTML = data;
                            return true;
                        }
                    </script>
                    <?php
                }
            }
            $tbl = null;
            $this->id = $tabid;
            $returntopage_varname = $tabid.'_returntopage';
            if(isset($_GET[$tabid.'page']))
            {
                $this->curpage = $_GET[$tabid.'page'];
                $_SESSION[$tabid.'_page'] = $this->curpage;
            }
            if(isset($_SESSION[$tabid.'_page']))
            {
                $this->curpage = $_SESSION[$tabid.'_page'];
            }
            elseif(is_array($tabs))
            {
                $ak = array_keys($tabs);
                $this->curpage = $ak[0];
            }
            $this->ttabs = $tabs;
            $containderid = $tabid.'Container'.self::$tabcount++;
            ?>
            <table<?php if(!is_null($width)){echo " width=\"$width\"";}?> border="0" cellpadding="0" cellspacing="0">
            <tr>
            <td>
                <div id="<?php echo $style; ?>">
                <ul>
                    <!-- CSS Tabs -->
                    <?php
                        foreach($tabs as $id => $tab_info)
                        {
                            $caption = $tab_info[0];
                            $url = $tab_info[1];
                            $url = str_replace("{1}", $tabid."page=$id", $url);
                            $current = null;
                            $ajax = null;
                            if($this->curpage == $id)
                            {
                                $current = 'id="current"';
                            }
                            if($with_ajax)
                            {
                                $ajax = "onclick=\"return LoadAjaxTabItem('$containderid','$tabid','$id');\"";
                            }
                            echo "<li $current><a $ajax href=\"$url\"><span>$caption</span></a></li>";
                        }
                    ?>
                </ul>
                </div>
            </td>
            </tr>
            </table>
            <?php
            $this->tablisttbl = new html_table(1, 'tablist', '100%');
            $this->tablisttbl->newcol();
        }
        public function end()
        {
            $this->tablisttbl->end();

        }
        public function tab()
        {
            return $this->curpage;
        }
    }

    // обработчик управляющей инструкции «tablist»
    function ShowTabList($parameters)
    {
        $tabname = $parameters['name'];
        $with_ajax = false;
        $width = null;
        $style = 'tabs0';
        if(array_key_exists('width', $parameters))
        {
            $width = $parameters['width'];
        }
        if(array_key_exists('style', $parameters))
        {
            $style = $parameters['style'];
        }
        if(array_key_exists('ajax', $parameters))
        {
            switch($parameters['ajax'])
            {
                case '1':case 'true':case 'yes':case '':
                    $with_ajax = true;
                    break;
                default:
                    $with_ajax = false;
                    break;
            }
            $with_ajax = $parameters['ajax'] == 'yes';
        }
        global $mikron;
        $tabcontrol = $mikron->Queries->Query('TABCONTROL', null, new Criterion('code', $tabname));
        $tabcontrol = $tabcontrol->fetch();
        $tabs = $tabcontrol->tabs->value;
        $tabarray = Array();
        if($tabs instanceof QueryClass)
        {
            while($tab = $tabs->fetch())
            {
                if($tab->disabled->value == 0)
                {
                    $tabarray[$tab->code->value] = Array($tab->name->value, $tab->link->value);
                }
            }
        }
        $cabtab = new tab_list($tabcontrol->code->value, $tabarray, $with_ajax, $width, $style);
        if($tabs instanceof QueryClass)
        {
            $tabs->reset();
            $current_tab_code = $cabtab->tab();
            while($tab = $tabs->fetch())
            {
                if($tab->code->value == $current_tab_code)
                {
                    echo $tab->content->value;
                    break;
                }
            }
        }
        $cabtab->end();
        if(isset($_REQUEST['ajax']))
        {
            $mikron->Page->EndBody(false);
            exit;
        }
    }

?>